/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package braipetro.client.base;

import braipetro.client.base.FrmBase;
import braipetro.data_access.DataSet;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JList;
import javax.swing.JTextField;

/**
 *
 * @author manasses
 */
public class DataBinding {
    private JComponent cmp;
    private String field;
    private String tableSource;
    private String keySource;
    private String labelSource;
    private FrmBase owner;

    public DataBinding(FrmBase owner) {
        this.owner = owner;
    }
    
    public JComponent getCmp() {
        return cmp;
    }

    public void setCmp(JComponent cmp) {
        this.cmp = cmp;
    }

    public String getField() {
        return field;
    }

    public void setField(String field) {
        this.field = field;
    }

    public String getTableSource() {
        return tableSource;
    }

    public void setTableSource(String tableSource) {
        this.tableSource = tableSource;
    }

    public String getKeySource() {
        return keySource;
    }

    public String getLabelSource() {
        return labelSource;
    }

    public void setLabelSource(String labelSource) {
        this.labelSource = labelSource;
    }

    public void setKeySource(String keySource) {
        this.keySource = keySource;
    }
    
    public void populateComponent() {
        DataSet dts = new DataSet();
        dts.setConnection(owner.getDataSet().getConnection());
        
        dts.setSql(String.format("select %s, %s from %s ",keySource, labelSource, tableSource));
        dts.open();
        dts.first();
        if (cmp instanceof JComboBox) {
            int nrows = dts.getNumRows();
            for (int i = 0; i < nrows; i++) {
                
                ((JComboBox)cmp).addItem(dts.getValue(labelSource));
                dts.next();
            }
        } else if (cmp instanceof JList) {
            
        }
        
        dts.close();
    } 
}
